import axios from "axios";
import global from "../utils/Global.js"
import { Message } from "element-ui";


const request = axios.create({
    // baseURL:'http://localhost:1200',
    baseURL:global.baseURL,
    // baseURL: 'http://111.61.250.175:16870/K3Cloud',
    timeout: 60000,
    headers: { 'Content-Type': 'application/json' },
})

//请求拦截器
request.interceptors.request.use(config => {
    let token = sessionStorage.getItem('kdservice-sessionid')
    token && (config.headers['kdservice-sessionid'] = token)
    // console.log(config.data,"请求拦截器","token : "+token)
    return config
});
//响应拦截器
request.interceptors.response.use(response => {
    if (Object.hasOwnProperty.call(response, 'status')) {
        if (response.status == '200') {
            if (Object.hasOwnProperty.call(response.data, 'Result')) {
                if (response.data.Result.ResponseStatus.IsSuccess) {
                    // Message({
                    //     type:"success",
                    //     message:'成功',
                    //     duration:2*1000
                    // })
                    return response.data
                }
                else {
                    // alert('响应失败')
                    let errors = response.data.Result.ResponseStatus.Errors
                    let errMsg = ''
                    errors.forEach(item => {
                        errMsg += item.Message
                    })
                    Message({
                        message: errMsg,
                        type: "error",
                        duration: 3 * 1000
                    })
                    console.log(response, "响应拦截器的返回内容")
                    // 判断返回的内容有没有formid
                    if (JSON.parse(response.config.data).formid != null || JSON.parse(response.config.data).formid != undefined) {
                        console.log(JSON.parse(response.config.data).formid, "响应拦截器-请求的formid的内容")
                        console.log(response.config.url, "响应拦截器-请求的url")
                        // 判断formid是否是PKTL_BidCustomer
                        if (response.config.url == "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc"
                            && JSON.parse(response.config.data).formid == "PKTL_BidCustomer") {
                            return response.data
                        }
                    }
                    // return Promise.reject(response.data.Result.ResponseStatus.Errors)
                }
            }
            else {
                return response.data
            }
        }
    }
    else {

        return Promise.reject(new Error())
    }
}, err => {
    console.log('错误' + err)
    return Promise.reject(err)
});

export default request